package com.template.common;


import java.io.Serializable;
import java.util.List;

/**
 * 分页实体封装
 * @param <T>
 */
public class PageData<T> implements Serializable {
    protected final List<T> items;
    protected final int totalItems;


    public List<T> getItems() {
        return this.items;
    }

    public int getTotalItems() {
        return this.totalItems;
    }

    public PageData(int size, List<T> items) {
        this.totalItems = size;
        this.items = items;
    }

    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append('[');
        result.append(this.totalItems).append(',');
        return result.toString();
    }

    public static int calcFirstItemIndexOfPage(int page, int pageSize, int total) {
        if (total == 0) {
            return 0;
        } else {
            if (page < 1) {
                page = 1;
            }

            int first = (page - 1) * pageSize;
            return first;
        }
    }
}
